package com.gaoh.modules.base.service.impl;

import com.gaoh.modules.base.entity.FileInfo;
import com.gaoh.modules.base.mapper.FileInfoMapper;
import com.gaoh.modules.base.service.IFileInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 文件信息
 * @Author: jeecg-boot
 * @Date: 2020-01-06
 * @Version: V1.0
 */
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements IFileInfoService {


    @Value("${project.path.upload}")
    private String path;

    /**
     * 文件上传
     *
     * @param request
     * @param fileInfo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String uploadImg(HttpServletRequest request, FileInfo fileInfo) throws Exception {
        if (System.getProperty("os.name").contains("win") || System.getProperty("os.name").contains("Win")) {
            path = path.split("\\?")[0];
        } else {
            path = path.split("\\?")[1];
        }
        //文件的相对路劲

        String nowDay = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File file = new File(path + File.separator + "files" + File.separator + nowDay);
        if (!file.exists()) {
            file.mkdirs();// 创建文件根目录
        }

        //文件相对路劲
        String filePath = File.separator + nowDay;

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获取上传文件对象
        MultipartFile mf = multipartRequest.getFile("file");

        if (mf != null && StringUtils.isNotBlank(mf.getOriginalFilename())) {

            //文件名
            String filename = mf.getOriginalFilename();

            //创建文件
            File nfile = new File(filename);
            //文件写入路劲
            String url = file + File.separator + nfile;
            //相对路劲
            filePath = File.separator + "files" + filePath + File.separator + nfile;

            byte[] files = mf.getBytes();

            //写入文件
            FileOutputStream out = new FileOutputStream(url);
            out.write(files);
            out.flush();
            out.close();
            System.gc();

            //写入文件信息
            FileInfo fileModel = new FileInfo();
            fileModel.setFileName(filename);
            fileModel.setFileOriginalName(filename);
            fileModel.setFilePath(url);
            fileModel.setFileRelativePath(filePath);
            //文件大小 KB
            fileModel.setFileSize(new BigDecimal(mf.getSize()).divide(new BigDecimal("1024"), 2, BigDecimal.ROUND_HALF_UP));
            fileModel.setFileSuffix(filename.substring(filename.lastIndexOf(".") + 1).toUpperCase());
            fileModel.setFileType(filename.substring(filename.lastIndexOf(".") + 1).toUpperCase());
            this.save(fileModel);
        }


        return filePath;
    }
}
